﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Controls;
using OFX.Aggregator.Infrastructure;

namespace OFX.Aggregator.WebScraping.Implementation.BESnet.UI.Wpf
{
	/// <summary>
	/// Interaction logic for BESnetCreditCardAccountSetupUserControl.xaml
	/// </summary>
	public partial class BESnetCreditCardAccountSetupUserControl : UserControl, IStatementSourceParametersProvider
	{
		public BESnetCreditCardAccountSetupUserControl()
		{
			InitializeComponent();
		}

		public IList<string> DisabledFields
		{
			get
			{
				var disabledFields = new List<string>();
				if (!this.AccountNumber.IsEnabled)
					disabledFields.Add(this.AccountNumber.Name);
				if (!this.AccountUserName.IsEnabled)
					disabledFields.Add(this.AccountUserName.Name);
				if (!this.AccountPassword.IsEnabled)
					disabledFields.Add(this.AccountPassword.Name);
				if (!this.CurrentStatementNumber.IsEnabled)
					disabledFields.Add(this.CurrentStatementNumber.Name);
				if (!this.CurrentStatementStartDate.IsEnabled)
					disabledFields.Add(this.CurrentStatementStartDate.Name);
				return disabledFields;
			}
			set
			{
				foreach (string disabledFieldName in value)
				{
					switch (disabledFieldName)
					{
						case "AccountNumber":
							this.AccountNumber.IsEnabled = false;
							break;
						case "AccountUserName":
							this.AccountUserName.IsEnabled = false;
							break;
						case "AccountPassword":
							this.AccountPassword.IsEnabled = false;
							break;
						case "CurrentStatementNumber":
							this.CurrentStatementNumber.IsEnabled = false;
							break;
						case "CurrentStatementStartDate":
							this.CurrentStatementStartDate.IsEnabled = false;
							break;
					}
				}
			}
		}

		#region IStatementSourceParametersProvider Members

		public IDictionary<string, string> GetParameters()
		{
			return new Dictionary<string, string>
			       	{
			       		{"AccountNumber", this.AccountNumber.Text},
			       		{"AccountUserName", this.AccountUserName.Text},
			       		{"AccountPassword", this.AccountPassword.Password},
			       		{"CurrentStatementNumber", (int.Parse(this.CurrentStatementNumber.Text)).ToString(NumberFormatInfo.InvariantInfo)},
			       		{"CurrentStatementStartDate", ((DateTime)this.CurrentStatementStartDate.SelectedDate).ToBinary().ToString(NumberFormatInfo.InvariantInfo)}
			       	};
		}

		public void SetParameters(IDictionary<string, string> parameters)
		{
			if (parameters == null) throw new ArgumentNullException("parameters");

			this.AccountNumber.Text = parameters["AccountNumber"];
			this.AccountUserName.Text = parameters["AccountUserName"];
			this.AccountPassword.Password = parameters["AccountPassword"];
			this.CurrentStatementNumber.Text = int.Parse(parameters["CurrentStatementNumber"], NumberFormatInfo.InvariantInfo).ToString();
			this.CurrentStatementStartDate.SelectedDate = new DateTime(long.Parse(parameters["CurrentStatementStartDate"], NumberFormatInfo.InvariantInfo));
		}

		#endregion
	}
}